document.addEventListener('DOMContentLoaded', function() {
  const apiKeyInput = document.getElementById('apiKey');
  const modelSelect = document.getElementById('modelSelect');
  const saveButton = document.getElementById('save');
  const statusDiv = document.getElementById('status');

  // 加载保存的设置
  chrome.storage.sync.get(['qwenApiKey', 'qwenModel'], function(result) {
    if (result.qwenApiKey) {
      apiKeyInput.value = result.qwenApiKey;
    }
    if (result.qwenModel) {
      modelSelect.value = result.qwenModel;
    }
  });

  // 保存设置
  saveButton.addEventListener('click', function() {
    const apiKey = apiKeyInput.value.trim();
    const selectedModel = modelSelect.value;
    
    if (!apiKey) {
      showStatus('请输入API Key', false);
      return;
    }

    chrome.storage.sync.set({
      qwenApiKey: apiKey,
      qwenModel: selectedModel
    }, function() {
      showStatus('设置已保存', true);
    });
  });

  function showStatus(message, success) {
    statusDiv.textContent = message;
    statusDiv.className = 'status ' + (success ? 'success' : 'error');
    statusDiv.style.display = 'block';
    
    setTimeout(function() {
      statusDiv.style.display = 'none';
    }, 3000);
  }
}); 